package com.yksj.healthtalk.ui.home;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.RootFragment;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.views.CirclePageIndicator;
import com.yksj.healthtalk.utils.SharePreUtils;
import com.yksj.healthtalk.utils.ViewUtils;
/**
 * 
 * @author jack_tang
 *	我的社区
 */
public class MyCommunityFragment extends RootFragment implements OnClickListener {

	private View view;
	private TabsPagerAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.my_community_fragment_layout, null);
		initUI(view);
		return view;
	}
	
	private void initUI(View view){
		ViewPager mViewPager = (ViewPager)view.findViewById(R.id.pager);
		CirclePageIndicator indicator = (CirclePageIndicator)view.findViewById(R.id.indicator);
		adapter = new TabsPagerAdapter();
		mViewPager.setAdapter(adapter);
		indicator.setViewPager(mViewPager);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		queryDataCount();
	}
	
	private void queryDataCount(){
		HttpRestClient.doHttpMyZoneCount("20",SmartFoxClient.getLoginUserId(),SharePreUtils.fatchNewsReadedLastId(),SharePreUtils.fatchInterestWallId(),new JsonsfHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);//新闻：80
				try{
					if(response.getInteger("30")==0){
						ViewUtils.setGone(adapter.friendNumberCount,true);
					}else{
						ViewUtils.setGone(adapter.friendNumberCount,false);
						adapter.friendNumberCount.setText(meSageCount( response.getInteger("30")));
					}
					
					if(response.getInteger("40")==0){
						ViewUtils.setGone(adapter.salonNumberCount,true);
					}else{
						ViewUtils.setGone(adapter.salonNumberCount,false);
						adapter.salonNumberCount.setText(meSageCount( response.getInteger("40")));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}
	
	
	//更新未读消息条数界面
	private String meSageCount(int count){
		String readCount;
		if(count!=0){
			readCount=count >100 ? "99+":String.valueOf(count);
		}else{
			readCount="0";
		}
		return readCount;
	}
	
	private class TabsPagerAdapter extends PagerAdapter{
		final List<View> views = new ArrayList<View>();
		View v1;
		Button friendNumberCount;
		Button salonNumberCount;
		public TabsPagerAdapter() {
			LayoutInflater layoutInflater =LayoutInflater.from(getActivity());
			v1 = layoutInflater.inflate(R.layout.my_community_ui_fragment_one,null);
			v1.findViewById(R.id.my_friend).setOnClickListener(MyCommunityFragment.this);
			TextView myfriend= (TextView) v1.findViewById(android.R.id.text1);
			//判断,如果是医生,把我的病友改成我的朋友
			if(SmartFoxClient.getLoginUserInfo().isDoctor())
				myfriend.setText("我的朋友");
			v1.findViewById(R.id.my_salon).setOnClickListener(MyCommunityFragment.this);
			friendNumberCount=(Button) v1.findViewById(R.id.firend_number_text);
			salonNumberCount=(Button) v1.findViewById(R.id.salon_number_text);
			views.add(v1);
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public int getCount() {
			return views.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View item = views.get(position);
			container.addView(item);
			return item;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed(v);
			break;
		case R.id.my_friend://我的病友
			intent = new Intent(getActivity(),FriendListActivity.class);
			startActivity(intent);
			break;
		case R.id.my_salon://我的话题
			intent = new Intent(getActivity(),SalonListActivity.class);
			this.startActivity(intent);
			break;
		}
		
	}
}
